﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Project.MODEL;
using Project.DAL;
using Project.DAL.System;
using System.Dynamic;
using Newtonsoft.Json;
using System.Security.Cryptography;

namespace Project.BLL.System
{
    public class EmployeeBll
    {
        EmployeeDal dal = new EmployeeDal();

        /// <summary>
        /// 分页获取员工数据
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public string GetEmployeeList(int page, int limit, string name)
        {
            var datas = dal.GetEmployee();
            var list = from d in datas
                       select new
                       {
                           d.emID,
                           d.emName,
                           d.emSex,
                           d.phone,
                           d.loginID,
                           d.headImg,
                           d.status,
                           d.roleID,
                           d.Roles.roleName,
                           d.Roles.deptID,
                           d.Roles.Department.deptName,
                           cmdName = d.Employee2.emName,
                           lastTime = d.lastTime.Year+"-"+ (d.lastTime.Month>9?d.lastTime.Month.ToString():"0"+ d.lastTime.Month) +"-"+ (d.lastTime.Day>9? d.lastTime.Day.ToString():"0"+d.lastTime.Day)
                       };

            //条件查询
            if (!string.IsNullOrWhiteSpace(name))
            {
                list = list.Where(d => d.emName.Contains(name));
            }

            //分页
            var minlist = list.OrderBy(d => d.emID).Skip((page - 1) * limit).Take(limit);

            //组装layui 中  table 所需的数据格式
            dynamic data = new ExpandoObject();
            data.code = 0;
            data.msg = "";
            data.count = list.Count();
            data.data = minlist;

            return JsonConvert.SerializeObject(data);
        }

        /// <summary>
        /// 添加员工
        /// </summary>
        /// <param name="emp"></param>
        /// <returns></returns>
        public string AddEmployee(Employee emp) {

            //统一定义返回数据格式
            dynamic data = new ExpandoObject();
            var datas  = dal.GetEmployee().Where(e=>e.loginID==emp.loginID).FirstOrDefault();
            if (datas!=null)
            {
                data.code = -1;
                data.msg = "已经存在该账户！";
            }
            else
            {
                emp.pwd = md5(emp.pwd); //加密密码
                emp.headImg = emp.emSex == "男" ? "/img/boy.png" : "/img/girl.png";
                int num = dal.AddEmployee(emp);
                data.code = num;
                data.msg = num > 0 ? "添加成功！" : "添加失败！";
            }
            return JsonConvert.SerializeObject(data);
        }

        /// <summary>
        /// 修改员工
        /// </summary>
        /// <param name="emp"></param>
        /// <returns></returns>
        public string UpdateEmployee(Employee emp) {

            int num = dal.UpdateEmployee(emp);

            //统一定义返回数据格式
            dynamic data = new ExpandoObject();
            data.code = num;
            data.msg = num > 0 ? "修改成功！" : "修改失败！";

            return JsonConvert.SerializeObject(data);
        }

        /// <summary>
        /// 批量删除员工
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public string DeleteAll(string ids)
        {
            var datas = dal.GetEmployee();
            string[] idd = ids.Split(',');
            var list = new List<Employee>();
            foreach (var i in idd)
            {
                int id = int.Parse(i);
                list.Add(datas.Where(d => d.emID == id).FirstOrDefault());
            }

            int num = dal.DeleteAll(list);

            //统一定义返回数据格式
            dynamic data = new ExpandoObject();
            data.code = num;
            data.msg = num > 0 ? "删除成功！" : "删除失败！";

            return JsonConvert.SerializeObject(data);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="pwd"></param>
        /// <param name="newpwd"></param>
        /// <returns></returns>
        public string ChangePassword(int uid, string pwd,string newpwd)
        {
            var datas = dal.GetEmployee();

            var emp = datas.Where(e => e.emID == uid).FirstOrDefault();

            //统一定义返回数据格式
            dynamic data = new ExpandoObject();
            if (emp.pwd == md5(pwd))
            {
                emp.pwd = md5(newpwd);
                int num = dal.UpdateEmployee(emp);
                data.code = num;
                data.msg = num > 0 ? "修改成功,请重新登录！" : "修改失败！";
            }
            else
            {
                data.code = -1;
                data.msg ="旧密码错误！";
            }
            return JsonConvert.SerializeObject(data);
        }

        /// <summary>
        /// 修改头像
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="imgpath"></param>
        /// <param name="newpwd"></param>
        /// <returns></returns>
        public string ChangeHeadImg(int uid, string imgpath)
        {
            var datas = dal.GetEmployee();

            var emp = datas.Where(e => e.emID == uid).FirstOrDefault();

            //统一定义返回数据格式
            dynamic data = new ExpandoObject();
            if (emp.headImg != imgpath)
            {
                emp.headImg=imgpath;
                int num = dal.UpdateEmployee(emp);
                data.code = num;
                data.msg = num > 0 ? "修改成功!" : "修改失败！";
            }
            else
            {
                data.code = -1;
                data.msg = "无变动！";
            }
            return JsonConvert.SerializeObject(data);
        }

        /// <summary>
        /// MD5加密密码
        /// </summary>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public string md5(string pwd)
        {
            MD5 md5 = MD5.Create();
            return BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(pwd))).Replace("-", null);//加密输入进来的密码
        }
    }
}
